// 获取每一天
function getEveryDay(start, end) {
  let dateList = []
  const startTime = new Date(start)
  const endTime = new Date(end)

  while (endTime.getTime() - startTime.getTime() >= 0) {
    const year = startTime.getFullYear()
    const month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1
    const day = startTime.getDate().toString().length == 1 ? '0' + startTime.getDate() : startTime.getDate()
    dateList.push(year + '-' + month + '-' + day)
    startTime.setDate(startTime.getDate() + 1)
  }
  return dateList
}

// 获取本月第一天日期
function getFirstDate4ThisMonth() {
  const y = new Date().getFullYear() //获取年份
  let m = new Date().getMonth() + 1 //获取月份
  const d = '01'
  m = m < 10 ? '0' + m : m //月份补 0

  return [y, m, d].join('-')
}

const pattern = /(?<=\/)(\d(\/|$))/g
// 获取当前日期
function getToday() {
  const date = new Date().toLocaleDateString()
  const result = date.replace(pattern, '0$1').replace(/\//g, '-')
  return result
}

function getWeekAgoDate() {
  const time = new Date().getTime() - 7 * 24 * 60 * 60 * 1000
  const targetTime = new Date(time).toLocaleDateString()
  const weekAgoDate = targetTime.replace(pattern, '0$1').replace(/\//g, '-')
  return weekAgoDate
}

exports.getEveryDay = getEveryDay
exports.getFirstDate4ThisMonth = getFirstDate4ThisMonth
exports.getToday = getToday
exports.getWeekAgoDate = getWeekAgoDate
